package com.powerwyx.shortlink.admin.controller;

import com.powerwyx.shortlink.admin.common.convention.result.Result;
import com.powerwyx.shortlink.admin.common.convention.result.Results;
import com.powerwyx.shortlink.admin.pojo.dto.GroupSaveDTO;
import com.powerwyx.shortlink.admin.pojo.dto.SortGroupDTO;
import com.powerwyx.shortlink.admin.pojo.dto.UpdateGroupDTO;
import com.powerwyx.shortlink.admin.pojo.vo.GroupVO;
import com.powerwyx.shortlink.admin.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王艺锡
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/shortlink/admin/v1/group")
public class GroupController {

    @Autowired
    private GroupService groupService;


    /**
     * 新增分组
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody GroupSaveDTO groupSaveDTO){
        groupService.saveGroup(groupSaveDTO.getName());
        return Results.success();
    }


    /**
     * 查询分组集合
     */
    @GetMapping("/list")
    public Result<List<GroupVO>> getGroups(){
        List<GroupVO> groupList = groupService.getGroups();
        return Results.success(groupList);
    }


    /**
     * 修改分组信息
     */
    @PutMapping("/update")
    public Result<Void> updateGroup(@RequestBody UpdateGroupDTO updateGroupDTO){
        groupService.updateGroup(updateGroupDTO);
        return Results.success();
    }


    /**
     * 删除短连接分组
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteGroup(@RequestParam("gid") String gid){
        groupService.deleteGroup(gid);
        return Results.success();
    }


    /**
     * 排序短连接分组
     */
    @PostMapping("/sort")
    public Result<Void> sortGroup(@RequestBody List<SortGroupDTO> sortGroupDTOList){
        groupService.sortGroup(sortGroupDTOList);
        return Results.success();
    }

}
